package com.lucy.flow.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.Validator;
import com.lucy.common.constant.HttpStatus;
import com.lucy.common.domain.ResponseDTO;
import com.lucy.flow.domain.entity.FlowDefine;
import com.lucy.flow.domain.entity.FlowNodeAssign;
import com.lucy.flow.model.FlowModel;
import com.lucy.flow.params.FlowNodeAssignCreateParams;
import com.lucy.flow.params.FlowNodeAssignQueryParams;
import com.lucy.flow.service.FlowDefineService;
import com.lucy.flow.service.FlowNodeAssignService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * @author kindear
 */
@Slf4j
@RestController
@RequestMapping("flow")
@Api(tags = "流程节点指派")
public class FlowNodeAssignController {
    @Autowired
    FlowNodeAssignService service;

    @Autowired
    FlowDefineService defineService;

    @PostMapping("assign")
    @ApiOperation(value = "指派新增",notes = "")
    public ResponseDTO<Object> assignCreate(@RequestBody @Validated FlowNodeAssignCreateParams params){
        // 获取流程定义
        String defineCode = params.getFlowDefineCode();
        Optional<FlowDefine> defineOp = defineService.getByDefineCode(defineCode);
        if (!defineOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND);
        }
        // 获取流程模型
        FlowDefine flowDefine = defineOp.get();
        FlowModel flowModel = flowDefine.getFlowModel();
        // 判断流程节点是否存在
        String flowNodeId = params.getFlowNodeId();
        if (Validator.isEmpty(flowModel.getNodeModel(flowNodeId))){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND,"流程不存在此节点");
        }
        // 新增对象
        FlowNodeAssign flowNodeAssign = new FlowNodeAssign();
        // @TODO 校验新增对象是否存在
        BeanUtil.copyProperties(params,flowNodeAssign);
        // 指定ID
        flowNodeAssign.setFlowDefineId(flowDefine.getId());
        // 写入
        return ResponseDTO.succData(
                service.saveOne(flowNodeAssign)
        );
    }

    @DeleteMapping("assign/{id}")
    @ApiOperation(value = "指派删除",notes = "")
    public ResponseDTO<Object> assignDelete(@PathVariable("id") String id){
        service.deleteOne(id);
        return ResponseDTO.succMsg("删除成功");
    }

    @GetMapping("assign")
    @ApiOperation(value = "指派列表",notes = "")
    public ResponseDTO<Object> assignListQuery(FlowNodeAssignQueryParams params){
        Sort sort;
        if(params.getOrder().equalsIgnoreCase("DESC")){
            sort = Sort.by(Sort.Direction.DESC, params.getSortBy());
        }else {
            sort = Sort.by(Sort.Direction.ASC, params.getSortBy());
        }

        // tips : 页码对应 - 1
        Pageable pageable = PageRequest.of(params.getPageNum() - 1 , params.getPageSize(), sort);

        FlowNodeAssign assign = new FlowNodeAssign();
        BeanUtil.copyProperties(params,assign, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<FlowNodeAssign> example = Example.of(assign);

        return ResponseDTO.succData(service.getAll(pageable,example));
    }

    @GetMapping("assign/{id}")
    @ApiOperation(value = "指派查询",notes = "")
    public ResponseDTO<Object> assignQuery(@PathVariable("id") String id){
        return ResponseDTO.succData(
                service.getOneUnwrap(id)
        );
    }

}
